package com.jcfk.eam.service.base;

import cn.hutool.core.lang.Assert;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamSopTypeDao;
import com.jcfk.eam.domain.dto.base.EamSopTypeDTO;
import com.jcfk.eam.domain.po.base.EamSopType;
import com.jcfk.eam.domain.query.base.EamSopTypeQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;

import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 标准作业类型
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-08
 */
@Service
public class EamSopTypeService extends BaseService<EamSopTypeDao, EamSopType> {

    /**
     * EamSopType分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamSopTypeDTO> queryByPage(DataPage<EamSopTypeDTO> page, EamSopTypeQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamSopType是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamSopTypeDTO dto) {
        QueryWrapper<EamSopType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("sop_type_code", dto.getSopTypeCode());
        queryWrapper.eq("asset_kind", dto.getAssetKind());
        queryWrapper.ne(StrUtil.isNotEmpty(dto.getId()),"id",dto.getId());
        int count = this.count(queryWrapper);
        Assert.isTrue(count == 0, "标准作业类型编号已经存在");
        return RetResult.ok();
    }

    /**
     * 插入标准作业类型
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamSopTypeDTO dto) {

        // 保存
        EamSopType item = new EamSopType();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新标准作业类型
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamSopTypeDTO dto) {

        // 保存
        EamSopType item = new EamSopType();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除标准作业类型(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamSopTypeDTO dto) {

        EamSopType item = new EamSopType();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamSopType> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamSopType item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取标准作业类型信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamSopTypeDTO get(String id) {
        // 查询数据
        EamSopType item = this.baseMapper.selectById(id);
        EamSopTypeDTO dto = new EamSopTypeDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 根据编号获取标准作业类型信息
     *
     * @param code 编号
     * @return StandardCodeDTO
     */
    public EamSopTypeDTO getByCode(String code) {
        // 查询数据
        QueryWrapper<EamSopType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamSopType::getSopTypeCode, code);
        EamSopType item = this.baseMapper.selectOne(queryWrapper);
        EamSopTypeDTO dto = new EamSopTypeDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamSopTypeDTO>
     */
    public List<EamSopTypeDTO> getList(EamSopTypeQuery query) {
        // 查询条件
        QueryWrapper<EamSopType> qw = new QueryWrapper<>();
        qw.lambda().eq(query.getAssetKind() != null, EamSopType::getAssetKind, query.getAssetKind());
        qw.lambda().eq(query.getTaskKind() != null, EamSopType::getTaskKind, query.getTaskKind());
        qw.lambda().eq(query.getEnabled() != null, EamSopType::getEnabled, query.getEnabled());
        qw.lambda().eq(query.getDeleted() != null, EamSopType::getDeleted, 0);
        // 查询数据
        List<EamSopType> list = super.list(qw);

        return convertList(list, EamSopTypeDTO.class);
    }

    /**
     * 根据主键获取标准作业类型信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamSopType item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

